package com.taxshare.seller.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.response.beans.Response;
import com.taxshare.seller.entity.GoodsManageEntity;
import com.taxshare.seller.entity.SellerInvoiceEntity;
import com.taxshare.seller.entity.SellerInvoiceRuleEntity;
import com.taxshare.seller.mapper.InvoicedMapper;
import com.taxshare.seller.mapper.SellerInvoiceRuleMapper;
import com.taxshare.seller.view.dto.InvoicedDTO;
import com.taxshare.seller.view.vo.EquipmentVO;
import com.taxshare.seller.view.vo.SellerInvoiceRuleReqVO;
import com.taxshare.seller.view.vo.SellerInvoiceRuleVO;
import com.taxshare.tools.token.UserInfoUtil;
import com.taxshare.tools.util.UUIDUitl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class SellerInvoiceRuleService extends ServiceImpl<SellerInvoiceRuleMapper, SellerInvoiceRuleEntity> {
    /**
     *  开票规则分页
     * @param query
     * @return
     */
    public Response getSellerInvoiceRuleList(PageRequest<SellerInvoiceRuleReqVO> query) {
        QueryWrapper<SellerInvoiceRuleEntity> queryWrapper = new QueryWrapper<>();
        String userID = UserInfoUtil.getUserID();
        SellerInvoiceRuleReqVO sellerInvoiceRuleReqVo = query.getInfo();
        String sellerTaxNo = sellerInvoiceRuleReqVo.getSellerTaxNo();
        String modelName = sellerInvoiceRuleReqVo.getModelName();
        //queryWrapper.eq("",userID);
        if(StrUtil.isNotBlank(sellerTaxNo)){
            queryWrapper.eq("seller_tax_no", sellerTaxNo);
        }

        if (StrUtil.isNotBlank(modelName)) {
            queryWrapper.like("model_name", modelName);
        }
        queryWrapper.orderByDesc("create_time");
        IPage<SellerInvoiceRuleVO> result = PageHelp.siamplePage(this, queryWrapper, query.getPage(), SellerInvoiceRuleVO.class);
        return Response.success(result);
    }

    /**
     * 新增开票规则
     * @param parames
     * @return
     */
    public Response saveSellerInvoiceRule(SellerInvoiceRuleVO parames){
        SellerInvoiceRuleEntity ellesrInvoiceRuleEntity = DozerBeanUtil.map(parames, SellerInvoiceRuleEntity.class);
        QueryWrapper<SellerInvoiceRuleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("model_name",parames.getModelName());
        queryWrapper.eq("seller_tax_no",parames.getSellerTaxNo());
        List modelList = this.list(queryWrapper);
        if(modelList!=null && modelList.size()!=0){
            return Response.error("T0001","已存在的模板名称！");
        }else{
            ellesrInvoiceRuleEntity.setDisable("0");
            ellesrInvoiceRuleEntity.setCreateTime(new Date());
            ellesrInvoiceRuleEntity.setUuid(UUIDUitl.UUID32(false));
            this.save(ellesrInvoiceRuleEntity);
            return Response.success();
        }
    }

    /**
     * 删除
     * @param uuid
     * @return
     */
    public Response delSellerInvoiceRuleById(String uuid){
        this.removeById(uuid);
        return Response.success();
    }

    /**
     * 修改
     * @param sellerInvoiceRuleVO
     * @return
     */
    public Response editSellerInvoiceRule(SellerInvoiceRuleVO sellerInvoiceRuleVO){
        SellerInvoiceRuleEntity sellerInvoiceRuleEntity = DozerBeanUtil.map(sellerInvoiceRuleVO, SellerInvoiceRuleEntity.class);
        sellerInvoiceRuleEntity.setUpdateTime(new Date());
        this.updateById(sellerInvoiceRuleEntity);
        return Response.success();
    }

    public Response disableSellerInvoiceRule(SellerInvoiceRuleVO sellerInvoiceRuleVO){
        String uuid = sellerInvoiceRuleVO.getUuid();
        String disable = sellerInvoiceRuleVO.getDisable();
        SellerInvoiceRuleEntity sire = this.getById(uuid);
        sire.setDisable(disable);
        this.updateById(sire);
        return Response.success();
    }
}
